package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;
/**
 * @author BAO
 * @version V1.0
 * @Package com.changgou.goods.controller
 * @date 2020/12/6 22:25
 * @description:
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/findAll")
    public Result findAll(){
        List<Brand> brandList = brandService.findAll();
        return Result.success("findAll success",brandList);
    }

    @GetMapping("/find/{brandId}")
    public Result findById(@PathVariable Integer brandId){
        Brand brand = brandService.findById(brandId);
        return Result.success("findById success",brand);
    }
    @PostMapping("/save")
    public Result save(@RequestBody Brand brand){
        int save = brandService.save(brand);
        return save>0?Result.success("save success",null):Result.fail("save fail");
    }

    @PutMapping("/update/{brandId}")
    public Result update(@RequestBody Brand brand,@PathVariable Integer brandId){
        brand.setId(brandId);
        int update = brandService.update(brand);
        return update>0?Result.success("save success",null):Result.fail("save fail");
    }

    @DeleteMapping("/delete/{brandId}")
    public Result deleteById(@PathVariable Integer brandId){
        int delete = brandService.deleteById(brandId);
        return delete>0?Result.success("save success",null):Result.fail("save fail");
    }

    @GetMapping("/search")
    public Result findCondition(@RequestParam Map<String, Object> searchMap){
        List<Brand> brandList = brandService.findCondition(searchMap);
        return Result.success("findCondition success",brandList);
    }

    @GetMapping("/search/{currentPage}/{size}")
    public Result findByPage(@PathVariable int currentPage,
                             @PathVariable int size){
        Page<Brand> brandPage = brandService.findByPage(currentPage, size);
        return Result.success("pageAll success",new PageResult<Brand>(brandPage.getTotal(),brandPage.getResult()));

    }

    @GetMapping("/searchCondition/{currentPage}/{size}")
    public Result findByPageAndCondition(@RequestParam Map<String ,Object> searchMap,
                                         @PathVariable int currentPage,
                                         @PathVariable int size){
        Page<Brand> Page= brandService.findByPageAndCondition(searchMap, currentPage, size);
        return Result.success("pageCondition success",new PageResult<Brand>(Page.getTotal(),Page.getResult()));
    }

    @GetMapping("/findListByCategoryName")
    public  Result findListByCategoryName(String categoryName){
        List<Map> list = brandService.findListByCategoryName(categoryName);
        return Result.success("findListByCategoryName success",list);
    }



}
